<?php

namespace app\index\controller;

use app\admin\model\About;
use app\admin\model\Article;
use app\admin\model\Newest;
use app\admin\model\Onlinepayment;
use app\admin\model\Paymentnotice;
use app\admin\model\Result;
use app\admin\model\Schedule;
use app\common\controller\Frontend;
use app\common\library\Token;
use think\Request;
use think\Response;
use think\Session;
use think\Validate;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 首页
     *
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        $schedule = $this->_getSchedule();
        $result = $this->_getResult(10);
        $articles = $this->_getArticle();
        $news = $this->_getNewest();
        return $this->view->fetch('index', [
            'schedule' => $schedule,
            'result' => $result,
            'article' => $articles,
            'news' => $news
        ]);
    }

    /**
     * 最新新聞
     *
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function _getNewest()
    {
        return Newest::where(['status' => 1])->order('id', 'desc')->select();
    }
    /**
     * 賽程數據
     *
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function _getSchedule()
    {
        return Schedule::limit(10)->order('id', 'desc')->select();
    }

    /**
     * 最新賽果
     *
     * @param $limit
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function _getResult($limit)
    {
        return Result::limit($limit)->order('id', 'desc')->select();
    }

    /**
     * 首頁新聞
     *
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function _getArticle()
    {
        return Article::where(['status' => 1])->order('id', 'desc')->select();
    }

    /**
     * 最新快訊內容
     *
     * @param Request $request
     * @param $id
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function newsDetail(Request $request, $id)
    {
        $infos = Newest::get($id);
        $list = Newest::where(['status' => 1])->order('id', 'desc')->field('id, title')->select();
        return $this->view->fetch('news', [
            'title' => '新聞快訊',
            'category' => 'newsDetail',
            'news' => $list,
            'infos' => $infos
        ]);
    }

    /**
     * 運彩分析王
     *
     * @param Request $request
     * @param $id
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function article(Request $request, $id)
    {
        $infos = Article::get($id);
        $list = Article::where(['status' => 1])->order('id', 'desc')->field('id, title')->select();
        return $this->view->fetch('news', [
            'title' => '運彩分析王',
            'category' => 'article',
            'news' => $list,
            'infos' => $infos
        ]);
    }

    /**
     * 線上付款
     *
     * @param Request $request
     * @param $id
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function payment(Request $request, $id)
    {
        $infos = [];
        if ($id > 0) {
            $infos = Onlinepayment::get($id);
        }
        $list = Onlinepayment::order('id', 'desc')->field('id, title')->select();
        if (!empty($list) && $id == 0) {
             $infos = Onlinepayment::get($list[0]->id);
        }
        return $this->view->fetch('news', [
            'title' => '線上付款',
            'category' => 'payment',
            'news' => $list,
            'infos' => $infos
        ]);
    }

    /**
     * 付款通知
     *
     * @param Request $request
     * @param $id
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function payNotice(Request $request, $id)
    {
        $infos = [];
        if ($id > 0) {
            $infos = Paymentnotice::get($id);
        }
        $list = Paymentnotice::order('id', 'desc')->field('id, title')->select();
        if (!empty($list) && $id == 0) {
            $infos = Paymentnotice::get($list[0]->id);
        }
        return $this->view->fetch('news', [
            'title' => '付款通知',
            'category' => 'payNotice',
            'news' => $list,
            'infos' => $infos
        ]);
    }

    /**
     * 客戶中心
     *
     * @param Request $request
     * @param $id
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function about(Request $request, $id)
    {
        $infos = [];
        if ($id > 0) {
            $infos = About::get($id);
        }
        $list = About::order('id', 'desc')->field('id, title')->select();
        if (!empty($list) && $id == 0) {
            $infos = About::get($list[0]->id);
        }
        return $this->view->fetch('news', [
            'title' => '客服中心',
            'category' => 'about',
            'news' => $list,
            'infos' => $infos
        ]);
    }

    /**
     * 注册会员
     */
    public function register()
    {

        if ($this->request->isPost()) {
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $passwordConfirm = $this->request->post('password_confirm');
            $email = $this->request->post('email');
            $mobile = $this->request->post('mobile', '');
            // $captcha = $this->request->post('captcha');
            $token = $this->request->post('__token__');
            $rule = [
                'username'  => 'require|length:3,30',
                'password'  => 'require|length:6,30|confirm',
                'email'     => 'require|email',
                'mobile'    => 'regex:/^1\d{10}$/',
                // 'captcha'   => 'require|captcha',
                '__token__' => 'token',
            ];

            $msg = [
                'username.require' => '用戶賬號不能為空',
                'username.length'  => '賬號名在3-30個字之間',
                'password.require' => '密碼不能為空',
                'password.length'  => '密碼在6-30個字之間',
                'password.confirm'  => '確認密碼必須一致',
                'captcha.require'  => '驗證碼不能為空',
                'captcha.captcha'  => '驗證碼錯誤',
                'email'            => 'Email錯誤',
                'mobile'           => '手機號碼錯誤',
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
                'password_confirm'  => $passwordConfirm,
                'email'     => $email,
                'mobile'    => $mobile,
                // 'captcha'   => $captcha,
                '__token__' => $token,
            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error($validate->getError());
            }
            if ($this->auth->register($username, $password, $email, $mobile)) {
                $this->success('註冊成功！', url('/'));
            } else {
                $this->error($this->auth->getError());
            }
        }
        return $this->view->fetch();
    }

    /**
     * 登录
     *
     * @return bool|string|\think\response\Json
     * @throws \think\Exception
     */
    public function login()
    {
        if ($this->request->isPost()) {
            $account = $this->request->post('account');
            $password = $this->request->post('password');
            $token = $this->request->post('__token__');
            $rule = [
                'account' => 'require|length:3,50',
                'password' => 'require|length:6,30',
                '__token__' => 'token',
            ];

            $msg = [
                'account.require' => '賬號不能為空',
                'account.length' => 'Account must be 3 to 50 characters',
                'password.require' => '密碼不能為空',
                'password.length' => '密碼在3-30個字之間',
            ];
            $data = [
                'account' => $account,
                'password' => $password,
                '__token__' => $token,
            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if ( ! $result) {
                $this->error($validate->getError());
            }
            if ($this->auth->login($account, $password)) {
                $this->success('登錄成功！', url('user/vip'));
            } else {
                $this->error($this->auth->getError());
            }
        }
        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    function logout()
    {
        Session::clear();
        $this->redirect(url('/'));
    }

}
